package parkshop.pmcourse.service.impl;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import parkshop.pmcourse.domain.FavoriteProduct;
import parkshop.pmcourse.domain.FavoriteSaller;
import parkshop.pmcourse.domain.Product;
import parkshop.pmcourse.domain.Shop;
import parkshop.pmcourse.dto.PageBean;
import parkshop.pmcourse.mapping.FavoriteProductMapper;
import parkshop.pmcourse.mapping.FavoriteSallerMapper;
import parkshop.pmcourse.mapping.ProductMapper;
import parkshop.pmcourse.mapping.ShopMapper;
import parkshop.pmcourse.service.FavoriteService;
import parkshop.pmcourse.utils.ConstantUtil;

@Service
public class FavoriteServiceImpl implements FavoriteService{

	@Autowired
	private FavoriteProductMapper favoriteProductMapper;
	@Autowired
	private FavoriteSallerMapper favoriteSallerMapper;
	@Autowired
	private ProductMapper productMapper;
	@Autowired
	private ShopMapper shopMapper;
	
	@Override
	public boolean saveFavoriteProduct(FavoriteProduct favoriteProduct) {
		// TODO Auto-generated method stub
		// 避免重复
		List<FavoriteProduct> list=favoriteProductMapper.selectByCustomerId(favoriteProduct.getCustomerId());
		if(list!=null)
			for(FavoriteProduct product:list)
				if(favoriteProduct.getCustomerId()==product.getCustomerId()&&favoriteProduct.getProductId()==product.getProductId())
					return false;
				
		int rows=favoriteProductMapper.insertSelective(favoriteProduct);
		return rows==1?true:false;
	}

	@Override
	public PageBean getFavoriteProductByCustomerId(int pageNumber,Long id) {
		// TODO Auto-generated method stub
		PageBean pageBean=new PageBean();
		List<Product> productList=new ArrayList<Product>();
		int count=favoriteProductMapper.selectCountByCustomerId(id);
		pageBean.setId(id);
		pageBean.setStartindex((pageNumber-1)*ConstantUtil.pageSize);
		pageBean.setPageSize(ConstantUtil.pageSize);
		List<FavoriteProduct> list= favoriteProductMapper.selectPageByCustomerId(pageBean);
		if(list!=null){
		for(FavoriteProduct favorite:list){
			Product product=productMapper.selectByPrimaryKey(favorite.getProductId());
			productList.add(product);
		}
		}
		
		pageBean.setRecordList(productList);
		pageBean.setRecordCount(count);
		pageBean.setCurrentPage(pageNumber);
		pageBean.setPageCount(count%ConstantUtil.pageSize==0?count/ConstantUtil.pageSize:count/ConstantUtil.pageSize+1);
		return pageBean;
	}

	@Override
	public PageBean getFavoriteShopByCustomerId(int pageNumber,Long id) {
		PageBean pageBean=new PageBean();
		List<Shop> list=new ArrayList<Shop>();
		int count=favoriteSallerMapper.selectCountByCustomerId(id);
		pageBean.setId(id);
		pageBean.setStartindex((pageNumber-1)*ConstantUtil.pageSize);
		pageBean.setPageSize(ConstantUtil.pageSize);
		List<FavoriteSaller> favoriteList=favoriteSallerMapper.selectPageByCustomerId(pageBean);
		if(favoriteList!=null){
			for(FavoriteSaller favorite:favoriteList){
				Shop shop=shopMapper.selectByPrimaryKey(favorite.getShopId());
				list.add(shop);
			}
		}
		
		pageBean.setRecordList(list);
		pageBean.setCurrentPage(pageNumber);
		pageBean.setRecordCount(count);
		pageBean.setPageCount(count%ConstantUtil.pageSize==0?count/ConstantUtil.pageSize:count/ConstantUtil.pageSize+1);
		return pageBean;
	}

	@Override
	public boolean deleteFavoriteProduct(Long productId, Long userId) {
		// TODO Auto-generated method stub
		PageBean pageBean=new PageBean();
		pageBean.setId(userId);
		pageBean.setSecondId(productId);
		int rows=favoriteProductMapper.deleteByCustomerAndProductId(pageBean);
		return rows==1?true:false;
	}

	@Override
	public boolean saveFavoriteSaller(FavoriteSaller favoriteSaller) {
		// 避免重复
		List<FavoriteSaller> list=favoriteSallerMapper.selectByCustomerId(favoriteSaller.getCustomerId());
		if(list!=null)
		for(FavoriteSaller saller:list)
			if(favoriteSaller.getCustomerId()==saller.getCustomerId()&&favoriteSaller.getShopId()==saller.getShopId())
				return false;
		
		
		int rows=favoriteSallerMapper.insertSelective(favoriteSaller);
		return rows==1?true:false;
	}

	@Override
	public boolean deleteFavoriteSaller(Long shopId, Long userId) {
		// TODO Auto-generated method stub

		PageBean pageBean = new PageBean();
		pageBean.setId(userId);
		pageBean.setSecondId(shopId);
		int rows=favoriteSallerMapper.deleteByCustomerAndShopId(pageBean);
		return rows==1?true:false;
	}

}
